/*
 * MenuBarDelegate.cpp  0.4.0 / Dec 05, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "MenuBarDelegate.h"

#include "MenuDelegate.h"

// MenuBarDelegate :: Protected Constructor. -----------------------------------

/*!
 * \brief Constructs new MenuBarDelegate object.
 *
 * \param env the JNIEnv pointer
 * \param obj the MenuBar java object
 */
MenuBarDelegate::MenuBarDelegate(JNIEnv *env, jobject obj)
  : ContainerDelegate(env, obj, new QMenuBar())
{
  // empty constructor body
}

// MenuBarDelegate :: Native Calls. --------------------------------------------

/*!
 * \brief Inserts child.
 *
 * \param env the JNIEnv pointer
 * \param childDelegate the child delegate
 * \param index the menu item index
 */
void MenuBarDelegate::insertChild(JNIEnv *env, jlong childDelegate, jint index)
{
  Q_ASSERT(env);
  SWING_REENTRANT(insertChild, SWING_ENV_ARG, JLONG_ARG(childDelegate),
                  JINT_ARG(index))
  QAction *before = 0;
  if ((index >= 0) && (index < qMenuBar()->actions().size())) {
    before = qMenuBar()->actions().at(index);
  }
  qMenuBar()->insertAction(before, COMPONENT_CAST(childDelegate)->qAction());
  COMPONENT_CAST(childDelegate)->setParentDelegate(this);
}

/*!
 * \brief Removes child.
 *
 * \param env the JNIEnv pointer
 * \param childDelegate the child delegate
 */
void MenuBarDelegate::removeChild(JNIEnv *env, jlong childDelegate)
{
  Q_ASSERT(env);
  SWING_REENTRANT(removeChild, SWING_ENV_ARG, JLONG_ARG(childDelegate))
  qMenuBar()->removeAction(COMPONENT_CAST(childDelegate)->qAction());
  COMPONENT_CAST(childDelegate)->setParentDelegate(0);
}

// MenuBarDelegate :: Static Native Calls. -------------------------------------

/*!
 * \brief Returns new instance of the delegate object
 *
 * \param env the JNIEnv pointer
 * \param obj the Swing object
 * 
 * \return new instance of the delegate object
 */
jlong MenuBarDelegate::newInstance(JNIEnv *env, jobject obj)
{
  if (JNISwingWrapper::isSwingThread(env)) {
    return reinterpret_cast<jlong>(new MenuBarDelegate(env, obj));
  }
  return SwingUtilitiesDelegate::invokeLongAndWait(env, obj, createMID());
}
